package java_DesignPattern.StructuralPattern.FlyweightPattern;

import java.util.HashMap;
import java.util.Map;

/**
 * 享元工厂,单例设计
 */
public class FlyweightFactory {

    //享元池
    private Map<String,AbstractShape> map;

    //初始化享元池
    private FlyweightFactory(){
        map = new HashMap<>();
        map.put("I",new IShape());
        map.put("L",new LShape());
        map.put("O",new OShape());
    }

    //懒汉式单例模式(线程安全)
    static class Singleton{
        private static final FlyweightFactory factory = new FlyweightFactory();
    }

    public static FlyweightFactory getInstance(){
        return Singleton.factory;
    }

    //获取图形
    public AbstractShape getShape(String key){
        return map.get(key);
    }
}
